const {con} = require('../db/db');
const { getTextrueList, getModelImgsList } = require('../model/images');

async function collectModel(modelid,userid,create_time) {
    const sql = `insert into model_collect(modelid,userid,create_time) values(${modelid},${userid},"${create_time}");`;

    await con.execute(sql);
}

async function cancelCollectModel(modelid,userid) {
    const sql = `delete from model_collect where modelid=${modelid} and userid=${userid};`;

    await con.execute(sql);
}

async function collectTextrue(textrueid,userid,create_time) {
    const sql = `insert into textrue_collect(textrueid,userid,create_time) values(${textrueid},${userid},"${create_time}");`;

    await con.execute(sql);
}

async function cancelCollectTextrue(textrueid,userid) {
    const sql = `delete from textrue_collect where textrueid=${textrueid} and userid=${userid};`;

    await con.execute(sql);
}

async function updateState(id,type) {
    switch(type) {
        case 'model_collect':
            const sql1 = `update modelimg set state=1 where id=${id};`;
            await con.execute(sql1);
            return getModelImgsList(1);
        case 'model_cancelCollect':
            const sql2 = `update modelimg set state=0 where id=${id};`;
            await con.execute(sql2);
            return getModelImgsList(1);
        case 'textrue_collect':
            const sql3 = `update textrue set state=1 where id=${id};`;
            await con.execute(sql3);
            return getTextrueList(1);
        case 'textrue_cancelCollect':
            const sql4 = `update textrue set state=0 where id=${id};`;
            await con.execute(sql4);
            return getTextrueList(1);
    }
}

async function getMyLikeList(userid) {
    const sql1 = `select workid from work_like where userid=${userid};`;

    let [rows1] = await con.execute(sql1);

    for(let i=0;i<rows1.length;i++) {
        const sql2 = `select * from works where id=${rows1[i].workid};`;

        let [rows2] = await con.execute(sql2);

        rows1[i] = rows2[0];
    }

    return rows1;
}

async function getMyModelCollect(userid,page) {
    const sql1 = `select modelid from model_collect where userid=${userid};`;

    let [rows1] = await con.execute(sql1);

    for(let i=0;i<rows1.length;i++) {
        const sql2 = `select * from modelimg where id=${rows1[i].modelid} order by id limit ${(page-1)*10},10`;

        let [rows2] = await con.execute(sql2);

        rows1[i] = rows2[0];
    }

    const total = `select * from model_collect;`;

    let [totals] = await con.execute(total);

    return {data:rows1,total:totals.length}
}

async function getMyTextrueCollect(userid,page) {
    const sql1 = `select textrueid from textrue_collect where userid=${userid};`;

    let [rows1] = await con.execute(sql1);

    for(let i=0;i<rows1.length;i++) {
        const sql2 = `select * from textrue where id=${rows1[i].textrueid} order by id limit ${(page-1)*10},10`;

        let [rows2] = await con.execute(sql2);

        rows1[i] = rows2[0];
    }

    const total = `select * from textrue_collect;`;

    let [totals] = await con.execute(total);

    return {data:rows1,total:totals.length}
}

async function getMyWorkCollect(userid) {
    const sql1 = `select workid from work_collect where userid=${userid};`;

    let [rows1] = await con.execute(sql1);

    for(let i=0;i<rows1.length;i++) {
        const sql2 = `select * from works where id=${rows1[i].workid};`;

        let [rows2] = await con.execute(sql2);

        rows1[i] = rows2[0];
    }

    return rows1;
}

async function getMyUserCollect(userid) {
    const sql1 = `select followid from user_collect where userid=${userid};`;

    let [rows1] = await con.execute(sql1);

    for(let i=0;i<rows1.length;i++) {
        const sql2 = `select * from user where id=${rows1[i].followid};`;

        let [rows2] = await con.execute(sql2);

        rows1[i] = rows2[0];
    }

    return rows1;
}

module.exports={
    collectModel,
    cancelCollectModel,
    collectTextrue,
    cancelCollectTextrue,
    updateState,
    getMyLikeList,
    getMyModelCollect,
    getMyTextrueCollect,
    getMyWorkCollect,
    getMyUserCollect
}